## this code written for history research.
# class's language written by Deutsche
# item' language written by En
# Content wrritten by native.

from qlib.data import dbobj, Cache
import requests
from bs4 import BeautifulSoup as Bs
import os

HISTORY_DB = os.path.expanduser("~/Research/history.db")
HISTORY_RES = os.path.expanduser("~/Research/Res/")
if not os.path.exists(os.path.dirname(HISTORY_DB)):
    os.mkdir(os.path.dirname(HISTORY_DB))
if not os.path.exists((HISTORY_RES)):os.mkdir(HISTORY_RES)

DB_COMMON = Cache(HISTORY_DB)


class Zeit(dbobj):
    # alias 
    # ac_time
    # pro_time
    # simple_time
    def __repr__(self):
        return self.pro_time + "|"+self.alias

class Person(dbobj):
    # people
    # alias
    def __repr__(self):
        return self.name+"|"+self.alias

class Berichten(dbobj):
    
    @property
    def When(self):
        return list(DB_COMMON.query(Zeit, simple_time=self.simple_time))
        
    # ara
    # people  ",".join(peoples) # text
    # places  ",".join(places) # text
    # jahreszahl text
    # report  text
    # when  when_id id this is most import!

class Ara(dbobj):
    def index(self, jahreszahl):
        return list(DB_COMMON.query(Berichten, jahreszahl= self.jahreszahl))

def wikidown(key, path=HISTORY_RES):
    if os.path.exists(os.path.join(path, key+".html")):
        return Bs(open(os.path.join(path, key+".html")), 'lxml')

    s = requests.Session()
    s.proxies = {
        'http':'socks5://127.0.0.1:1080',
        'https':'socks5://127.0.0.1:1080',
    }
    res = s.get('https://zh.wikipedia.org/wiki/%s' % key)
    if res.status_code == 200:
        with open(os.path.join(path, key+".html"), 'w') as fp:fp.write(res.text)
    return Bs(res.text, 'lxml')